/*
 * Copyright (C) 2007  Vianney le Clément
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package fsahoraires.gui.pages;

import static fsahoraires.util.Util.loadStream;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import fsahoraires.gui.MainWindow;

public abstract class SimpleHtmlTemplatePage extends GeneratedHtmlPage {

	private static Map<String, String> cache = new HashMap<String, String>();

	private String template;

	public SimpleHtmlTemplatePage(String title, String image,
			String templateName) {
		super(title, image);
		template = cache.get(templateName);
		if (template == null) {
			InputStream stream = MainWindow.class.getResourceAsStream("res/"
					.concat(templateName));
			if (stream != null) {
				try {
					template = loadStream(stream);
					cache.put(templateName, template);
				} catch (IOException e) {
					template = "<html><body>${content}</body></html>";
				}
			}
		}
	}

	@Override
	protected String load() {
		return template.replace("${content}", loadContent());
	}

	protected abstract String loadContent();

}
